home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / grafix / raytracing / raylab / source / texture.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  4KB  |  189 lines

  1. /*
  2.     name:    texture.c
  3.  
  4.     Textures
  5.     --------
  6.  
  7.     Texture-calculation and texture-handling.
  8.  
  9. */
  10.  
  11.  
  12. #include  "defs.h"
  13.  
  14.  
  15. void CreateDefTexture(TEXTURE *t)
  16. {
  17.     t->CMap.Colors[0].r=1.0; t->CMap.Colors[0].g=0.3; t->CMap.Colors[0].b=0.0;
  18.     t->CMap.Colors[1].r=0.0; t->CMap.Colors[1].g=0.0; t->CMap.Colors[1].b=0.0;
  19.     t->CMap.LastBound=1;
  20.     t->Pattern=PATTERN_NONE;
  21.     t->Reflect.r=t->Reflect.g=t->Reflect.b=0.0;
  22.     t->Filter.r=t->Filter.g=t->Filter.b=0.0;
  23.     t->Ior=0.0;
  24.     t->Ambient=0.2;
  25.     t->Diffuse=0.8;
  26.     t->Phong=0.3; t->PhongSize=10.0;
  27.     ClearTransform(&t->Transform);
  28. }
  29.  
  30.  
  31.  
  32. void CopyTexture(TEXTURE *t2, TEXTURE *t1)
  33. {
  34.     long    i,lbound;
  35.  
  36.     lbound=t1->CMap.LastBound;
  37.     for(i=0;i<=lbound;i++) {
  38.         t2->CMap.Bounds[i]=t1->CMap.Bounds[i];
  39.         t2->CMap.Colors[i].r=t1->CMap.Colors[i].r;
  40.         t2->CMap.Colors[i].g=t1->CMap.Colors[i].g;
  41.         t2->CMap.Colors[i].b=t1->CMap.Colors[i].b;
  42.     }
  43.     t2->CMap.LastBound=lbound;
  44.     t2->Pattern=t1->Pattern;
  45.     t2->Reflect.r=t1->Reflect.r;
  46.     t2->Reflect.g=t1->Reflect.g;
  47.     t2->Reflect.b=t1->Reflect.b;
  48.     t2->Filter.r=t1->Filter.r;
  49.     t2->Filter.g=t1->Filter.g;
  50.     t2->Filter.b=t1->Filter.b;
  51.     t2->Ior=t1->Ior;
  52.     t2->Ambient=t1->Ambient;
  53.     t2->Diffuse=t1->Diffuse;
  54.     t2->Phong=t1->Phong;
  55.     t2->PhongSize=t1->PhongSize;
  56.     CopyTransform(&t2->Transform,&t1->Transform);
  57. }
  58.  
  59.  
  60.  
  61. void CopyColor(COLOR *c2, COLOR *c1)
  62. {
  63.     c2->r=c1->r; c2->g=c1->g; c2->b=c1->b;
  64. }
  65.  
  66. /**************************************************************
  67.  *
  68.  *     Here are some useful (more or less) texture-patterns.
  69.  *
  70.  **************************************************************/
  71.  
  72. void GetSurfaceColor(COLOR *Color, TEXTURE *t, POINT *ip)
  73. {
  74.     double    MapValue;
  75.     int    indx,foundindx,i;
  76.     COLORMAP *cm;
  77.     VECTOR    tempv;
  78.     POINT    temppoint;
  79.  
  80.     CopyPoint(&temppoint,ip);
  81.  
  82.     if((t->Transform.NumTransforms>0)&&(t->Pattern!=PATTERN_NONE)) {
  83.         for(i=t->Transform.NumTransforms-1L;i>=0L;i--) {
  84.         switch(t->Transform.Entry[i].Type) {
  85.             case TRANSFORM_SCALE:
  86.             temppoint.x=temppoint.x/t->Transform.Entry[i].Values.x;
  87.             temppoint.y=temppoint.y/t->Transform.Entry[i].Values.y;
  88.             temppoint.z=temppoint.z/t->Transform.Entry[i].Values.z;
  89.             break;
  90.             case TRANSFORM_MOVE:
  91.             temppoint.x=temppoint.x-t->Transform.Entry[i].Values.x;
  92.             temppoint.y=temppoint.y-t->Transform.Entry[i].Values.y;
  93.             temppoint.z=temppoint.z-t->Transform.Entry[i].Values.z;
  94.             break;
  95.             case TRANSFORM_ROTATE:
  96.             NegVector(&tempv,&t->Transform.Entry[i].Values);
  97.             RotatePoint(&temppoint,&tempv);
  98.             break;
  99.             case TRANSFORM_NONE:
  100.             default:
  101.             break;
  102.         }
  103.         }
  104.     }
  105.  
  106.     switch(t->Pattern) {
  107.         case PATTERN_CHECKER:
  108.         MapValue=PatternChecker(&temppoint);
  109.         break;
  110.         case PATTERN_CIRCLES:
  111.         MapValue=PatternCircles(&temppoint);
  112.         break;
  113.         case PATTERN_RINGS:
  114.         MapValue=PatternRings(&temppoint);
  115.         break;
  116.         case PATTERN_SPOTS:
  117.         MapValue=PatternSpots(&temppoint);
  118.         break;
  119.         case PATTERN_GRADIENT:
  120.         MapValue=PatternGradient(&temppoint);
  121.         break;
  122.         case PATTERN_NONE:
  123.         default:
  124.         MapValue=0.0;
  125.         break;
  126.     }
  127.  
  128.     cm=&t->CMap;
  129.     foundindx=-1;
  130.  
  131.     if(MapValue>EPSILON) {
  132.         indx=0;
  133.         while((indx<cm->LastBound)&&(foundindx!=0)) {
  134.         if((MapValue>=cm->Bounds[indx])&&(MapValue<=cm->Bounds[indx+1]))
  135.             foundindx=0;
  136.         else indx++;
  137.         }
  138.     }
  139.     if(foundindx==0) {
  140.         Color->r=(cm->Colors[indx].r*(cm->Bounds[indx+1]-MapValue)+cm->Colors[indx+1].r*(MapValue-cm->Bounds[indx]))/(cm->Bounds[indx+1]-cm->Bounds[indx]);
  141.         Color->g=(cm->Colors[indx].g*(cm->Bounds[indx+1]-MapValue)+cm->Colors[indx+1].g*(MapValue-cm->Bounds[indx]))/(cm->Bounds[indx+1]-cm->Bounds[indx]);
  142.         Color->b=(cm->Colors[indx].b*(cm->Bounds[indx+1]-MapValue)+cm->Colors[indx+1].b*(MapValue-cm->Bounds[indx]))/(cm->Bounds[indx+1]-cm->Bounds[indx]);
  143.     }
  144.     else {
  145.         Color->r=cm->Colors[0].r;
  146.         Color->g=cm->Colors[0].g;
  147.         Color->b=cm->Colors[0].b;
  148.     }
  149. }
  150.  
  151.  
  152. double PatternChecker(POINT *ip)
  153. {
  154.     return( (double)(((long)floor(ip->x+EPSILON)+(long)floor(ip->y+EPSILON)+(long)floor(ip->z+EPSILON))&1L) );
  155. }
  156.  
  157.  
  158. double PatternCircles(POINT *ip)
  159. {
  160.     return(fmod(sqrt(ip->x*ip->x+ip->y*ip->y+ip->z*ip->z),1.0));
  161. }
  162.  
  163. double PatternRings(POINT *ip)
  164. {
  165.     return(fmod(sqrt(ip->x*ip->x+ip->y*ip->y),1.0));
  166. }
  167.  
  168. double PatternSpots(POINT *ip)
  169. {
  170.     double    d;
  171.     POINT    dgrid;
  172.  
  173.     dgrid.x=ip->x-floor(ip->x+0.5);
  174.     dgrid.y=ip->y-floor(ip->y+0.5);
  175.     dgrid.z=ip->z-floor(ip->z+0.5);
  176.  
  177.     d=2.0*sqrt(dgrid.x*dgrid.x+dgrid.y*dgrid.y+dgrid.z*dgrid.z);
  178.     if(d>1.0) d=1.0;
  179.     return(d);
  180. }
  181.  
  182. double PatternGradient(POINT *ip)
  183. {
  184.     if(ip->z>0) return(fmod(fabs(ip->z),1.0));
  185.     else return(fmod(1.0-fabs(ip->z),1.0));
  186. }
  187.  
  188.  
  189.